#include "modulehplcdetector.h"
#include  <QDebug>
#include "protocol/hplc/qinfine/detector/hplcqinfinedetectorprotocol.h"
#include "common/hplcqinfinedefine.h"
#include "modulehplcdetectorparamiddefine.h"

ModuleHplcDetector::ModuleHplcDetector(QObject *parent)
    : BaseModule (parent)
{
    m_timer = new QTimer(this);
    m_timer->setInterval(BASE_TICK_MS);
    connect(m_timer, &QTimer::timeout, this, &ModuleHplcDetector::onTimeout);
}

ModuleHplcDetector::~ModuleHplcDetector()
{
    delete  m_timer;
}

bool ModuleHplcDetector::setConfig(int id, QVariant &var)
{

}

bool ModuleHplcDetector::getConfig(int id, QVariant &var)
{

}

bool ModuleHplcDetector::setParam(int id, QVariant &var)
{
    QMutexLocker locker(m_mutex);
    switch (id)
    {
        case ModuleHplcDetectorParamID_SoftwareVersion:
            return true;
        case ModuleHplcDetectorParamID_HardwareVersion:
            return true;
        case ModuleHplcDetectorParamID_BasicParam:
            return true;
        case ModuleHplcDetectorParamID_WaveCalibData:
            {
                QList<hplc::DetectorWaveCalibData> list;
                list = var.value<QList<hplc::DetectorWaveCalibData>>();
                m_param.systemParam.waveCalibTable = list;
            }
            return true;
        case ModuleHplcDetectorParamID_HomeOffset:
            {
                m_param.systemParam.homeOffset = var.toInt();
            }
            return true;
        case ModuleHplcDetectorParamID_WaveOffset:
            {
                m_param.systemParam.waveOffset = var.toUInt();
            }
            return true;
        case ModuleHplcDetectorParamID_DACZeroVoltage:
            return true;
        case ModuleHplcDetectorParamID_IntegralParam:
            {
                hplc::IntegralParam param = var.value<hplc::IntegralParam>();
                m_param.systemParam.integralParam = param;
            }
            return true;
        case ModuleHplcDetectorParamID_HomeIntegralParam:
            {
                hplc::IntegralParam param = var.value<hplc::IntegralParam>();
                m_param.systemParam.homeIntegralParam = param;
            }
            return true;
        case ModuleHplcDetectorParamID_AuUploadFrequency:
            {
                m_param.systemParam.auUploadFrequency = var.toUInt();
            }
            return true;
        case ModuleHplcDetectorParamID_UploadObject:
            {
                hplc::UploadObject object = static_cast<hplc::UploadObject>(var.toUInt());
                m_param.systemParam.uploadObject = object;
            }
            return true;
        default:
            return false;
    }
}

bool ModuleHplcDetector::getParam(int id, QVariant &var)
{
    QMutexLocker locker(m_mutex);
    switch (id)
    {
        case ModuleHplcDetectorParamID_SoftwareVersion:
            var.setValue(m_param.softwareVersion);
            return true;
        case ModuleHplcDetectorParamID_HardwareVersion:
            var.setValue(m_param.hardwareVersion);
            return true;
        case ModuleHplcDetectorParamID_BasicParam:
            var.setValue(m_param.basicParam);
            return true;
        case ModuleHplcDetectorParamID_WorkMode:
            var.setValue(m_param.systemParam.workMode);
            return true;
        case ModuleHplcDetectorParamID_WaveCalibData:
            var.setValue(m_param.systemParam.waveCalibTable);
            return true;
        case ModuleHplcDetectorParamID_HomeOffset:
            var.setValue(m_param.systemParam.homeOffset);
            return true;
        case ModuleHplcDetectorParamID_WaveOffset:
            var.setValue(m_param.systemParam.waveOffset);
            return true;
        case ModuleHplcDetectorParamID_DACZeroVoltage:
            var.setValue(m_param.systemParam.dacZeroVoltage);
            return true;
        case ModuleHplcDetectorParamID_IntegralParam:
            var.setValue(m_param.systemParam.integralParam);
            return true;
        case ModuleHplcDetectorParamID_HomeIntegralParam:
            var.setValue(m_param.systemParam.homeIntegralParam);
            return true;
        case ModuleHplcDetectorParamID_AuUploadFrequency:
            var.setValue(m_param.systemParam.auUploadFrequency);
            return true;
        case ModuleHplcDetectorParamID_UploadObject:
            var.setValue(m_param.systemParam.uploadObject);
            return true;
        default:
            return false;
    }
}

bool ModuleHplcDetector::setProtocolCmd(int id, QVariant &var)
{
    QMutexLocker locker(m_mutex);
    return m_protocol->setProtocolCmd(id, var);
}

bool ModuleHplcDetector::getProtocolCmd(int id, QVariant &var)
{
    QMutexLocker locker(m_mutex);
    return m_protocol->getProtocolCmd(id, var);
}

void ModuleHplcDetector::setPort(BasePort *port)
{
    QMutexLocker locker(m_mutex);
    if (m_port)
    {
        m_port->disconnect();
        m_port->deleteLater();
    }
    m_port = port;

    connect(port, &BasePort::receivePackets,
            this, &ModuleHplcDetector::onParse, Qt::QueuedConnection);
    connect(this, &ModuleHplcDetector::requestSend,
            port, &BasePort::onSend, Qt::QueuedConnection);
}

BasePort *ModuleHplcDetector::getPort(void)
{
    QMutexLocker locker(m_mutex);
    return m_port;
}

void ModuleHplcDetector::setProtocol(BaseProtocol *protocol)
{
    QMutexLocker locker(m_mutex);
    if (m_protocol)
    {
        m_protocol->disconnect();
        m_protocol->deleteLater();
    }
    m_protocol = protocol;
    connect(m_protocol, &BaseProtocol::requestSend, this, &ModuleHplcDetector::requestSend);
    connect(m_protocol, &BaseProtocol::parseCompleted, this, &ModuleHplcDetector::parseCompleted);
}

BaseProtocol *ModuleHplcDetector::getProtocol()
{
    QMutexLocker locker(m_mutex);
    return m_protocol;
}

void ModuleHplcDetector::setProtocolAddress(quint8 address)
{
    QMutexLocker locker(m_mutex);
    m_protocol->setConfig(0, address);
}

quint8 ModuleHplcDetector::getProtocolAddress(void)
{
    QMutexLocker locker(m_mutex);
    QVariant var;
    m_protocol->getConfig(0, var);
    return var.toUInt();
}

hplc::DetectorParam *ModuleHplcDetector::getParam()
{
    QMutexLocker locker(m_mutex);
    return &m_param;
}

void ModuleHplcDetector::onTimeout()
{
    QMutexLocker locker(m_mutex);
    hplc::qinfine::HplcQinfineDetectorProtocol *protocol = static_cast<hplc::qinfine::HplcQinfineDetectorProtocol*>(m_protocol);
    protocol->onBaseTickTimeout();
}

void ModuleHplcDetector::onParse(QList<ProtocolPacket> packets)
{
    QMutexLocker locker(m_mutex);
    if (m_enable)
        m_protocol->onWork(packets);
}

void ModuleHplcDetector::onStart()
{
    QMutexLocker locker(m_mutex);
    if (m_enable && m_port->isOpen())
        m_timer->start();
}
